<template>
  <div class="app-container">


    <!-- vxe检索数据组件 -->
    <search-form
      ref="searchForm"
      :formSearchData.sync="formSearchData"
      @searchEventEmit="searchEventEmit"
      @resetSearchEventEmit="resetSearchEventEmit"
    >
    </search-form>


    <!-- 表格组件 -->
    <ex-base-table
      ref="exBaseTable"
      :tableColumn="tableColumn"
      :tableData="tableData"
      :loading.sync="loading"
      :handleColsWidth="220"
      :isVxeCheckbox="false"
      @handleRemoveEmit="handleRemoveEmit"
      @handleCreateEmit="handleCreateEmit"
      @handleUpdateEmit="handleUpdateEmit"
      @handleRmoveColEmit="handleRmoveColEmit"
      @customSortMethodEmit="customSortMethodEmit"
    >

      <!-- 表格工具组扩展插槽 -->
      <template v-slot:vxeToolbarExtend>
        <el-button size="mini" icon="el-icon-export" @click="handlePushExportUrl">导出</el-button>
      </template>


      <!-- 操作栏按钮扩展插槽 -->
      <template v-slot:handleExtend="{row}">
        &nbsp;
        <el-button v-if="true" size="mini" type="text" @click="viewSonTable(row)">
          价格设置
        </el-button>

        &nbsp;
        <el-popconfirm
          :confirm-button-text="$t('base.affirm')"
          :cancel-button-text="$t('base.cancel')"
          icon="el-icon-info"
          icon-color="red"
          title="确定发布？"
          @confirm="publishStatus(row)"
        >
          <el-button slot="reference" size="mini" type="text">发布</el-button>
        </el-popconfirm>
        &nbsp;
        <el-popconfirm
          :confirm-button-text="$t('base.affirm')"
          :cancel-button-text="$t('base.cancel')"
          icon="el-icon-info"
          icon-color="red"
          title="确定撤销？"
          @confirm="noPublishStatus(row)"
        >
          <el-button slot="reference" size="mini" type="text">撤销</el-button>
        </el-popconfirm>

      </template>
    </ex-base-table>


    <!-- 分页组件 -->
    <Pagination
      v-show="tablePage.totalResult>0"
      :total="tablePage.totalResult"
      :page.sync="tablePage.currentPage"
      :limit.sync="tablePage.pageSize"
      @pagination="pageFindList"
    />


    <!-- 表单提价组件 -->
    <submit-form
      ref="submitForm"
      :dialogFormVisible.sync="dialogFormVisible"
      :dialogStatus="dialogStatus"
      :formSubmitData="formSubmitData"
      :lpOptionscurr="lpOptionscurr"
      :lpOptions="lpOptions"
      :curvetypeOptions="curvetypeOptions"
      @handleFromUpdateDataEmit="handleFromUpdateDataEmit"
      @handleFromCreateDataEmit="handleFromCreateDataEmit"
      @resetFormDataEmit="resetFormSubmitData"
    >
    </submit-form>


    <el-dialog
      width="80%"
      :visible.sync="DialogSonTableVisible"
    >
      <ParamValMaintainTable
        ref="paramValMaintainTable"
        :currentRow="currentRow"
      />
    </el-dialog>


  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'

import {
  fetchListTransfercurve,
  findtransfercurvetest,
  findcurvetypeall,
  findtransfercurvederive,
  addTransfercurve,
  updateTransfercurve,
  deleteTransfercurve,
  batchDeleteTransfercurve,
  fetchListuserlp,
  fetchListcurr,
  releaseTransfercurve,
  revokeTransfercurve
} from './service/baseApi'
//日期模糊查询
import XEUtils from 'xe-utils'
// 基础表格组件
import ExBaseTable from '@/components/VxeTable/ExBaseTable'
// 分页
import Pagination from '@/components/Pagination'
// 文章表单
import SubmitForm from './components/SubmitForm'
// 检索表单
import SearchForm from './components/SearchForm'
//信息提示框
import {MessageBox, Message} from 'element-ui'

import ParamValMaintainTable from './components/ParamValMaintainTable'
import resetFormSubmitData from '@/views/mixins/resetFormSubmitData'

export default {
  name: 'vxe-basetable',
  mixins:[resetFormSubmitData],
  components: {
    //PageHeaderLayout,
    ExBaseTable,
    Pagination,
    SubmitForm,
    SearchForm,
    ParamValMaintainTable
  },
  // 定义属性
  data() {
    return {
      // 检索条件
      formSearchData: {
        cv_code: '',
        cv_name: '',
        data_date: [],

      },
      // 排除条件
      sortField: '',
      sortFlag: '',

      /* 表单相关 */
      dialogFormVisible: false, // 修改删除弹出框
      dialogStatus: '',
      // 表单数据对象
      formSubmitData: {
        id: "",
        cv_code: "",
        cv_name: '',
        curr_code: "",
        cv_type: "",
        lp_code: '',
        remark: '',
      },
      initFormSubmitData: {
        id: "",
        cv_code: "",
        cv_name: '',
        curr_code: "",
        cv_type: "",
        lp_code: '',
        remark: '',
      },
      lpOptions: [],
      lpOptionscurr: [],
      curvetypeOptions: [],
      /* 分页相关 */
      tablePage: {
        currentPage: 1, //当前页
        pageSize: 10,  // 每一页页数
        totalResult: 0
      },
      /* 表格相关 */
      tableColumn: [], // 表格字段
      loading: false, //表格loading特效
      tableData: [],  //表格数据

      DialogSonTableVisible: false,
      currentRow: {},

    } //datareturn-end

  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {


    /* API数据操作相关 */
    // 查询数据
    findList() {
      this.loading = true
      // 请求表格数据
      findtransfercurvetest({
        page: this.tablePage.currentPage, // 当前页
        pageSize: this.tablePage.pageSize, // 每页条数
        sortField: this.sortField,
        sortFlag: this.sortFlag,
        s_date: (XEUtils.isArray(this.formSearchData.data_date) && this.formSearchData.data_date.length === 2) ? this.formSearchData.data_date[0] : '',
        e_date: (XEUtils.isArray(this.formSearchData.data_date) && this.formSearchData.data_date.length === 2) ? this.formSearchData.data_date[1] : '',
        cv_code: this.formSearchData.cv_code,
        cv_name: this.formSearchData.cv_name,
      }).then(({data}) => {
        // 给表格数据赋值
        this.tableData = data.rows
        // 给分页总数赋值
        this.tablePage.totalResult = data.total
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      }, error => {
        // alert(error);
      })
    },
    findlp() {
      this.loading = true
      fetchListuserlp({}).then(({data}) => {

        this.lpOptions = data.rows
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      }, error => {
        // alert(error);
      })
    },
    findcurr() {
      this.loading = true
      fetchListcurr({}).then(({data}) => {

        this.lpOptionscurr = data.rows
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      }, error => {
        // alert(error);
      })
    },
    findcurvetype() {
      this.loading = true
      findcurvetypeall({}).then(({data}) => {
        this.curvetypeOptions = data.rows
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      }, error => {
        // alert(error);
      })
    },
    /* 分页组件相关 */
    // 分页事件（页码切换，每页页数切换）
    pageFindList() {

      this.findList()
    },
    /* 表格组件相关 */
    // 初始化表格字段
    initTableCols() {
      const tabCols = [  //动态增加的字段 传进来的
        // { field: 'te_date', title: '生效日期', width: 200 , showOverflow: true},
        {field: 'cv_code', title: '曲线编号', width: 300, sortable: true},
        {field: 'cv_name', title: '曲线名称', width: 200},
        {field: 'curr_name', title: '币种', width: 200},
        {field: 'tp_name', title: '期限方案', width: 200},
        {field: 'term_value', title: '期限值', width: 200},
        {field: 's_date', title: '生效日期', width: 200},
        {field: 'e_date', title: '失效日期', width: 200},
        {field: 'cv_typename', title: '拟合曲线方式', width: 300},
        {field: 'lp_name', title: '所属法人', width: 300},

        // { field: 'mod_us_code', title: '修改用户', width: 300 },
        // { field: 'mod_date', title: '修改时间', width: 300 },
        {
          field: 'status', title: '状态', width: 300, formatter(obj) {
            if (obj.cellValue == 2) {
              return "已发布"
            } else if (obj.cellValue == 1) {
              return "待发布"
            }
          }
        },
        {field: 'remark', title: '备注', width: 300}
      ]
      this.tableColumn = tabCols;
    },
    // 表格工具组-多选删除
    handleRemoveEmit(selectRecords) {
      // this.$refs.exBaseTable.$refs.xTable.removeCheckboxRow()
      batchDeleteTransfercurve({
        ...selectRecords
      }).then(({data}) => {
        this.findList()
      })
    },
    // 表格工具组-新增表单弹窗（需要表单组件）
    handleCreateEmit(type) {
      // 重置表单内容
      this.resetFormSubmitData()
      this.dialogStatus = type
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.submitForm.$refs['dataForm'].clearValidate()
      })
    },
    // 行内按钮-修改数据弹窗（需要表单组件）
    handleUpdateEmit(row, type) {
      this.formSubmitData = Object.assign({}, row) // copy obj
      console.log('XXDDFFthis.formSubmitData', this.formSubmitData);
      this.dialogStatus = type
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.submitForm.$refs['dataForm'].clearValidate()
      })
    },
    // 单行删除
    handleRmoveColEmit(row) {
      // this.$refs.exBaseTable.$refs.xTable.remove(row)
      deleteTransfercurve({
        id: row.id,
        m_id: row.m_id
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },

    /* 表单组件相关 */
    // 更新
    handleFromUpdateDataEmit() {
      updateTransfercurve({
        ...this.formSubmitData,  //检索条件
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },

    // 增加
    handleFromCreateDataEmit() {
      addTransfercurve({
        ...this.formSubmitData,  //检索条件
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },

    /* 检索相关 */
    // 检索
    searchEventEmit() {
      this.tablePage.currentPage = 1
      this.findList()
    },
    // 重置
    resetSearchEventEmit() {
      // 重置代码...
      this.tablePage.currentPage = 1
      this.findList()
    },
    /* 排序 */
    customSortMethodEmit(property, order) {
      this.sortField = property
      this.sortFlag = order
      this.findList()

    },

    /* 子表格弹出 */
    viewSonTable(row) {

      this.currentRow = Object.assign({}, row) // copy obj
      this.DialogSonTableVisible = true
    },

    //发布
    publishStatus(row) {
      releaseTransfercurve({
        id: row.ftcsid
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },
    // 撤销
    noPublishStatus(row) {
      revokeTransfercurve({
        id: row.ftcsid
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },
    /* 导出 url */
    handlePushExportUrl() {

      const XEUtilsLocat = XEUtils.locat()
      const hostName = XEUtilsLocat.hostname
      const protocol = XEUtilsLocat.protocol
      const s_date = (XEUtils.isArray(this.formSearchData.data_date) && this.formSearchData.data_date.length === 2) ? this.formSearchData.data_date[0] : ''
      const e_date = (XEUtils.isArray(this.formSearchData.data_date) && this.formSearchData.data_date.length === 2) ? this.formSearchData.data_date[1] : ''
      const cv_code = this.formSearchData.cv_code
      const cv_name = this.formSearchData.cv_name

      // 跳转示例1：
      // let routeUrl = this.$router.resolve({
      //   path: "/share",
      //   query: {id:96}
      // });
      // window.open(routeUrl.href, '_blank');

      //  findtransfercurvederive({
      //       page:this.tablePage.currentPage, // 当前页
      //       pageSize:this.tablePage.pageSize, // 每页条数
      //       sortField:this.sortField,
      //       sortFlag:this.sortFlag,
      //       ...this.formSearchData,  //检索条件
      //       }).then(({data}) => {
      //         // 给表格数据赋值
      //         this.tableData = data.rows
      //         // 给分页总数赋值
      //         this.tablePage.totalResult = data.total
      //         // 关闭加载特效
      //         setTimeout(() => {
      //             this.loading = false
      //         }, 0.5 * 1000)
      //     },error => {
      //       // alert(error);
      //     })
      // 跳转示例2：
      // window.open("http://www.baidu.com",'_blank') // 在新窗口打开外链接
      // 跳转示例3：
      window.location.href =
        protocol + '//' +
        hostName + ':7001/' + process.env.VUE_APP_DATA_IO_URI_PARAM +
        '/Transfercurve/findtransfercurvederive?s_date=' + s_date
        + '&e_date=' + e_date + '&cv_code=' + cv_code + '&cv_name=' + cv_name;  //在本页面打开外部链接
    }


  }, //method-end
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    // 测试功能-下拉分页表格
    // this.keyupEvent4()
    // 初始化表格字段
    this.initTableCols()
    // 初始化表格数据
    this.findList()

    this.findlp()
    this.findcurr()
    this.findcurvetype()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>


.my-dropdown4 {
  width: 600px;
  height: 300px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  box-shadow: 0 0 6px 2px rgba(0, 0, 0, 0.1);
}
</style>